<?php

/**
 * Arrow request context
 * 
 * @version 1.0
 * @license  GNU GPL
 * @author Artur Kmera <artur.kmera@arrowplatform.org> 
 * @todo Rozwinoc o ciastka i pliki, dodac wykrywanie typu wywołania
 */
class ArrowRequestContext extends ArrowObject implements ArrayAccess {
	
	/**
	 * Get vars
	 * @var array
	 */
	private $get = array();

	/**
	 * Post vars
	 * @var array
	 */
	private $post;

	/**
	 * Request type ( xml, post, get )
	 * @var string
	 */
	private $requestType;

	/**
	 * Request instance
	 * @var RequestContext
	 */
	private static $oInstance = null;
	
	/**
	 * From post to get vars
	 *
	 * @var Array
	 */
	private $postToGet = array();
	
	/**
	 * Variables to use in url if redirected
	 *
	 * @var Array
	 */
	private $registredUrlVars = array();

	/**
	 * Singleton
	 *
	 * @return ArrowRequestContext
	 */
	public static function getDefault(){
		if( self::$oInstance == null ){
			self::$oInstance = new ArrowRequestContext();
		}
		return self::$oInstance;
	}
	
	/**
	 * Constructor
	 *
	 */
	private function __construct(){
		if( !isset($_POST["arrowPost"]) )
      $this->_parse_post_to_get( $_POST, $_POST ) ;
			
		$this->post = $this->inputFilter( $_POST );
		$this->get = $this->inputFilter( $_GET );
	}
	
	
	private function _parse_post_to_get( &$branch, $request ) {
    foreach( $branch as $key => $val ) {
      $name = $key ;
      if( substr($key, 0,3) == "_d_" ){
					$name = substr($key, 3);
					$this->postToGet[$name] = $val;
					$branch[$name] = $val;
					unset( $branch[$key] );
			}
			if( is_array($val) ) $this->_parse_post_to_get( $branch[$name], $val ) ; 
    }
	}
	
	
	/**
	 * Returns get values
	 * 
	 * @param String $variableName
	 * @return mixed
	 */
	public function getGet( $variableName = false ){

		if( $variableName != false ){
			if(isset($this->get[$variableName])){
				return $this->get[$variableName];
			}else{
				//throw new ArrowCoreException( "[Request] Get var is not set: '$variableName'", 1 );
				return false;
			}
		}
		return $this->get;
	}
	
	/**
	 * Return post var
	 * 
	 * @param String $variableName
	 * @return mixed
	 */
	public function getPost( $variableName = false  ){
		if( $variableName != false ){
			if(isset($this->post[$variableName])){
				return $this->post[$variableName];
			}else{
				//throw new ArrowCoreException( "[Request] Post var is not set: '$variableName'", 2 );
				return false;
			}
		}
		return $this->post;
	}
	
	/**
	 * Return request var
	 * 
	 * @param String $variableName
	 * @return mixed
	 */
	public function getRequest( $variableName = false ){
		if( $variableName === false )
			return array_merge( $this->getPost(), $this->getGet() );
		$value = $this->getGet($variableName);
		if( $value === false )
			$value = $this->getPost($variableName);
		return $value;
	}
	
	
	
	/**
	 * Filter for input values
	 *
	 * @param $inputArray Array
	 * @return Array filtered values in array
	 * @todo implement filter
	 */
	private function inputFilter( $inputArray ){
		if(get_magic_quotes_gpc()){
			foreach ($inputArray as $key => $val){
				if( is_array( $val ) ){
					$inputArray[$key] = $this->inputFilter($val);
				}else{
					$inputArray[$key] = stripslashes($val);
				}
			}
		}
		return $inputArray;
	}
	
	/**
	 * Returns post to ger
	 *
	 * @return Array
	 */
	public function getPostToGet(){
		return $this->postToGet;
	}
	
	/**
	 * Returns registred url vars
	 */
	public function getRegistredUrlVars(){
		return $this->registredUrlVars;
	}

	/**
	 * Register var that will be used in next url ( if redirected)
	 *
	 * @param String $name
	 * @param String $value
	 */
	public function registerUrlVar( $name, $value ){
		$this->registredUrlVars[$name] = $value;
	}

	/**
	 * ArrayAcces implementaction
	 * 
	 * @param string $key
	 * @return boolean
	 */
	public function offsetExists($key) {
		if (isset ($_GET[$key])||isset ($_POST[$key]))
			return true;
		return false;
	}
	
	/**
	 * ArrayAcces implementaction
	 * 
	 * @param string $key
	 * @return mixed
	 */
	public function offsetGet($key) {
		if ($this->offsetExists($key)) {
			return $this->getRequest($key);
		} else {
			return false;
			throw new ArrowCoreException(array( "msg" => "[RequestContext] Offset doesn't exist'", "offset" => $key ), 0);
		}
	}
	
	/**
	 * ArrayAcces implementaction
	 * 
	 * @param string $key
	 * @param mixed value
	 */
	public function offsetSet($key, $value) {
		throw new ArrowCoreException(array( "msg" => "[RequestContext] Can't use Array acces to set  value'" ),0);
	}

	/**
	 * ArrayAcces implementaction
	 * 
	 * @param string $key
	 * @return mixed
	 */
	public function offsetUnset($key) {
		throw new ArrowCoreException(array( "msg" => "[RequestContext]  Can't use Array acces to unset  value'" ),0);
	}	
}

?>
